import Vue from 'vue'
import Vuex from 'vuex'


Vue.use(Vuex)

const store = new Vuex.Store({

    // 全局状态
    state: {
       userId: 0,
       username: ''
    },

    getters:{
        
        getUserId(){
            return localStorage.getItem("userId")
        },
        getUserName(){
            return localStorage.getItem("username")
        }

    },

    // 通过此方法改变值
    mutations:{
        // 设置登陆用户 userId
        setUserId(state,userId){
            localStorage.setItem("userId",userId)
            state.userId = userId;
        },

        // 设置登陆用户username
        setUserName(state,username){
            localStorage.setItem("username",username)
            state.username = username
        }
    },

    // 异步请求调用
    actions: {

    }

})

export default store